﻿using Inaction.ServiceContract;
using Inaction.Core;
using Inaction.Mapping;
using Inaction.Security;
using Inaction.Service.Client.WebService;
using Inaction.ServiceProxy;
using Inaction.Util;
using System;
using System.Security.Principal;

namespace Inaction.Service.Client.Proxy
{
    public class WebServiceProxy : IProxy
    {
        private WebService.WebService host = null;

        #region 构造
        public WebServiceProxy()
        { 
            host = new Client.WebService.WebService();
            host.Url = AppConfig.Instance.ServerUrl;
        }
        #endregion

        private static WebService.Request CreateRequest(object data = null, string assembly = null, string fullName = null)
        {
            var request = new WebService.Request();
            request.SerializerType = WebService.SerializeType.Binary;
            request.IsCompressed = true;
            RequestData requestData = null;
            if (data == null) requestData = new RequestData();
            else requestData = new RequestData(data, assembly, fullName);
            request.ObjData = ServiceContainer.GetService<IZip>().Compress(ServiceContainer.GetService<ISerializerHelper>().DefaultSerializer.Serialize(requestData));
            return request;
        }

        private static T GetData<T>(WebService.Response response) where T : class
        {
            if (response.ObjData == null || response.ObjData.Length == 0) return (T)null;
            byte[] data = null;
            if (response.IsCompressed)
            {
                data = ServiceContainer.GetService<IZip>().Decompress(response.ObjData);
            }
            var responseData =ServiceContainer.GetService<ISerializerHelper>().DefaultSerializer.Deserialize<ResponseData>(data);
            if (responseData.Exception != null)
                throw responseData.Exception;
            return (T)responseData.Data;
        }

        #region IProxy
        public void Login()
        {
            var response = host.Login(CreateRequest());
            Inaction.Security.Principal.Instance = GetData<Inaction.Security.Principal>(response);
        }
        public object Query(Type entityType, SqlStatement sql)
        {
            var response = host.Query(CreateRequest(sql, entityType.Assembly.GetName().Name, entityType.FullName));
            return GetData<ISavable>(response);
        }
        public object Save(ISavable obj)
        {
            var response = host.Query(CreateRequest(obj));
            return GetData<ISavable>(response);
        }
        public object Excute(SqlCommand command)
        {
            var response = host.Query(CreateRequest(command));
            return GetData<SqlCommand>(response);
        }
        public object Invoke(RemotingMethod method)
        {
            var response = host.Query(CreateRequest(method));
            return GetData<RemotingMethod>(response);
        }
        #endregion
    }

}